from itsdangerous import TimedJSONWebSignatureSerializer as Serializer

from mall import settings


def generic_openid_token(openid):

    # JWS

    # 1.创建序列化器
    serializer = Serializer(secret_key=settings.SECRET_KEY,expires_in=3600)

    # 2.组织数据
    data = {
        'openid':openid
    }
    # 3.加密处理
    openid_token = serializer.dumps(data)
    # 4. 加密之后还的数据是by

    return openid_token.decode()

from itsdangerous import BadTimeSignature,BadSignature
def check_access_token(token):
    # 创建序列化器
    serializer = Serializer(secret_key=settings.SECRET_KEY, expires_in=3600)
    try:
        data = serializer.loads(token)

    except BadSignature as e:
        return None

    openid = data.get('openid')
    return openid